#!/bin/bash

# Copyright 2014 Treode, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Build, test and package before merging into the master branch.
#
# This script works with the Jenkins service. Jenkins triggers the build whenever there is a push
# on a merge branch (that is a branch named 'merge/*'). It runs this script, and if it is
# successful, then Jenkins merges the change into the master branch. This script produces a
# directory with everything that needs to be put on oss.treode.com.
#
# Specifically, this script
# 1) Builds and tests the libraries for the Treode store.
# 2) Publishes those libraries locally.
# 3) Builds and tests the examples using the locally published libraries.
# 4) Builds the Scala docs.
# 5) Stages everything in 'stage' for ftp to oss.
#
# Actually putting the files on oss requires a password, so that has been left for a person.

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
. $DIR/scripts/build-functions.sh

# Number of seeds for pseudo-random tests.
export NSEEDS=10

clean

# Check that every version number is in sync across all build files.
version=`scripts/version-number \
  project/TreodeBuild.scala \
  demos/movies/project/MoviesBuild.scala`
expect-status 0

# If not a snapshot, check that this release is new.
case $version in
*-SNAPSHOT)
  ;;
*)
  if [ -n "`git tag -l release/$version`" ] ; then
    echo "Tag release/$version already exists"
    exit 1
  fi
  ;;
esac

log "Building $version"

#
# Compile.
#

# Compile, and also publish locally for the demos below.
echo-do ${SBT} +test:compile +publish-local

# Movies demo; it has multiple components.
(cd demos/movies; echo-do ${SBT} test:compile); expect-status 0
(cd demos/movies/webui; echo-do ${NPM} install --save); expect-status 0
(cd demos/movies/webui; echo-do ${GRUNT} dist); expect-status 0

#
# Test
#

echo-do ${SBT} test intensive:test

(cd demos/movies; echo-do ${SBT} test); expect-status 0

#
# Stage release.
#

# Server.
STAGE_JARS=stage/jars/${version}
echo-do ${SBT} server/assembly
echo-do mkdir -p ${STAGE_JARS}
echo-do cp server/target/scala-2.11/server.jar  ${STAGE_JARS}/server-${version}.jar

# Movies demo.
STAGE_MOVIES=stage/demos/movies/${version}
(cd demos/movies; echo-do ${SBT} assembly); expect-status 0
echo-do mkdir -p ${STAGE_MOVIES}
echo-do tar cz -f ${STAGE_MOVIES}/movies-ui-${version}.tgz -C demos/movies/webui/dist/ .
echo-do cp demos/movies/server/target/scala-2.11/movies-server.jar ${STAGE_MOVIES}/movies-server-${version}.jar
echo-do cp demos/movies/spark/target/scala-2.11/movies-spark.jar ${STAGE_MOVIES}/movies-spark-${version}.jar

#
# Packaging
#

# Debian package.
STAGE_DEB=stage/pkgs/debian
echo-do mkdir -p ${STAGE_DEB}
echo-do ${DIR}/scripts/package.sh -t debian -V ${version} -j ${STAGE_JARS}/server-${version}.jar -o ${STAGE_DEB}

# Docker image.
echo-do docker build --rm -t treode/demos-movies:${version} demos/movies

#
# Done
#

wrapup
